﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SettingsStorageAppConfig.cs" company="DevDigital">
//   DevDigital
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Settings.NET.Storage
{
    using System;
    using System.Configuration;

    /// <summary>
    /// Settings storage - app.config
    /// Provides retrieval of settings from the app.config application settings
    /// </summary>
    [Serializable]
    public class SettingsStorageAppConfig : ISettingsStorage
    {
        /// <summary>
        /// Gets the setting.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="scope">The scope.</param>
        /// <returns>The setting</returns>
        public ISetting<string> GetSetting(string key, string scope)
        {
            if (string.IsNullOrEmpty(key))
            {
                return null;
            }

            var value = ConfigurationManager.AppSettings[key];
            return new Setting<string>(key, value, value != null);
        }

        /// <summary>
        /// Sets the setting.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <param name="scope">The scope.</param>
        /// <exception cref="System.InvalidOperationException">Cannot set an application configuration setting</exception>
        public void SetSetting(string key, string value, string scope)
        {
            throw new InvalidOperationException("Cannot set an application configuration setting");
        }

        /// <summary>
        /// Removes the setting.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="scope">The scope.</param>
        /// <exception cref="System.InvalidOperationException">Cannot remove an application configuration setting</exception>
        public void RemoveSetting(string key, string scope)
        {
            throw new InvalidOperationException("Cannot remove an application configuration setting");
        }
    }
}